package com.example.wechat.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.wechat.R;
import com.example.wechat.model.Friend;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class FriendAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private static final int TYPE_HEADER = 0;
    private static final int TYPE_ITEM = 1;

    private List<Object> items;
    private Context context;
    private OnItemClickListener listener;

    public interface OnItemClickListener {
        void onItemClick(Friend friend);
        void onDeleteClick(Friend friend);
        void onMoveGroupClick(Friend friend);
    }

    public FriendAdapter(Context context, List<Friend> friendList) {
        this.context = context;
        this.items = new ArrayList<>();
        organizeFriendsByGroup(friendList);
    }

    private void organizeFriendsByGroup(List<Friend> friendList) {
        Map<String, List<Friend>> groupedFriends = new HashMap<>();
        
        // 按分组组织好友
        for (Friend friend : friendList) {
            String group = friend.getGroup();
            if (!groupedFriends.containsKey(group)) {
                groupedFriends.put(group, new ArrayList<>());
            }
            groupedFriends.get(group).add(friend);
        }

        // 将分组和好友添加到items列表
        for (Map.Entry<String, List<Friend>> entry : groupedFriends.entrySet()) {
            items.add(entry.getKey()); // 添加分组标题
            items.addAll(entry.getValue()); // 添加该分组下的好友
        }
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }

    @Override
    public int getItemViewType(int position) {
        return items.get(position) instanceof String ? TYPE_HEADER : TYPE_ITEM;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == TYPE_HEADER) {
            View view = LayoutInflater.from(context).inflate(R.layout.item_group_header, parent, false);
            return new GroupHeaderViewHolder(view);
        } else {
            View view = LayoutInflater.from(context).inflate(R.layout.item_friend, parent, false);
            return new FriendViewHolder(view);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof GroupHeaderViewHolder) {
            String groupName = (String) items.get(position);
            ((GroupHeaderViewHolder) holder).groupNameText.setText(groupName);
        } else if (holder instanceof FriendViewHolder) {
            Friend friend = (Friend) items.get(position);
            FriendViewHolder friendHolder = (FriendViewHolder) holder;
            
            friendHolder.nicknameText.setText(friend.getNickname());
            friendHolder.remarkText.setText(friend.getRemark());
            friendHolder.groupText.setText(friend.getGroup());

            friendHolder.itemView.setOnClickListener(v -> {
                if (listener != null) {
                    listener.onItemClick(friend);
                }
            });

            friendHolder.deleteButton.setOnClickListener(v -> {
                if (listener != null) {
                    listener.onDeleteClick(friend);
                }
            });

            friendHolder.moveGroupButton.setOnClickListener(v -> {
                if (listener != null) {
                    listener.onMoveGroupClick(friend);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return items.size();
    }

    public void updateData(List<Friend> newFriendList) {
        items.clear();
        organizeFriendsByGroup(newFriendList);
        notifyDataSetChanged();
    }

    static class GroupHeaderViewHolder extends RecyclerView.ViewHolder {
        TextView groupNameText;

        GroupHeaderViewHolder(View itemView) {
            super(itemView);
            groupNameText = itemView.findViewById(R.id.group_name_text);
        }
    }

    static class FriendViewHolder extends RecyclerView.ViewHolder {
        ImageView avatarImage;
        TextView nicknameText;
        TextView remarkText;
        TextView groupText;
        ImageView deleteButton;
        ImageView moveGroupButton;

        FriendViewHolder(View itemView) {
            super(itemView);
            avatarImage = itemView.findViewById(R.id.avatar_image);
            nicknameText = itemView.findViewById(R.id.nickname_text);
            remarkText = itemView.findViewById(R.id.remark_text);
            groupText = itemView.findViewById(R.id.group_text);
            deleteButton = itemView.findViewById(R.id.delete_button);
            moveGroupButton = itemView.findViewById(R.id.move_group_button);
        }
    }
} 